package queue;

public class Queue<T> {
    private Node first;// 栈顶（最近添加的元素）
    private Node last;// 栈尾
    private int N; // 元素数量

    public boolean isEmpty() {
        return first == null;
    }

    public int size() {
        return N;
    }

    public void enqueue(T t) {
        // 表尾添加元素
        Node oldNode = last;
        last = new Node();
        last.t = t;
        oldNode.next = null;
        if (isEmpty()) {
            first = last;
        } else {
            oldNode.next = last;
        }
        N++;
    }

    public T dequeue() {
        // 栈顶删除元素
        Node popNode = first;
        first = first.next;
        if (isEmpty()) {
            last = null;
        }
        N--;
        return popNode.t;
    }

    private class Node {
        // 定义节点的嵌套类
        T t;
        Node next;
    }
}
